package gui;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import javax.swing.JButton;
import javax.swing.JLabel;

import plugin.conditions.ConditionEditor;

/**
 * The Class ActPreAndPostCondWindow.
 */
public class ActPreAndPostCondWindow extends JFrame {

    /** The Constant serialVersionUID. */
    private static final long serialVersionUID = 6750147799809044784L;
    
    private static final String PROPERTY_TMP_DIR = "java.io.tmpdir";
    
    /** The content pane. */
    private JPanel contentPane;
    
    /** The table. */
    private JTable table;
    private JLabel lblNewLabel;

    /**
     * Launch the application.
     *
     * @param args the arguments
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ActPreAndPostCondWindow frame = new ActPreAndPostCondWindow();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public ActPreAndPostCondWindow() {
        setTitle("Action Conditions");
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setBounds(100, 100, 510, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        
        table = new JTable();
        table.setModel(new DefaultTableModel(
            new Object[][] {
            },
            new String[] {
                     "Action", "Pre-Condition", "Post-condition"
            }
        ) {
            /**
             * 
             */
            private static final long serialVersionUID = 7660297234535915717L;
            @SuppressWarnings("rawtypes")
            Class[] columnTypes = new Class[] {
                String.class, String.class, String.class
            };
            public Class getColumnClass(int columnIndex) {
                return columnTypes[columnIndex];
            }
        });
        
        javax.swing.JScrollPane jScrollPane1 = new javax.swing.JScrollPane();
        jScrollPane1.setViewportView(table);
        contentPane.add(jScrollPane1, BorderLayout.CENTER);
        
        JButton btnOk = new JButton("OK");
        contentPane.add(btnOk, BorderLayout.SOUTH);
        
        lblNewLabel = new JLabel("Please specify pre-condition and post-condition of each action(separate by ;):");
        contentPane.add(lblNewLabel, BorderLayout.NORTH);
        
        btnOk.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                okButtonClicked(evt);
            }
        });
    }
    
    protected void okButtonClicked(MouseEvent evt) {
        try {
            File destFolder = new File(ConditionEditor.projectPath + File.separator + "temp");
            if(!destFolder.exists()) {
                destFolder.mkdirs();
            }
            
            File destFile = new File(ConditionEditor.projectPath + File.separator + "temp" + File.separator + ConditionEditor.fileName + ".action");
            //write to file
            if(!destFile.exists()) {
                destFile.createNewFile();
            }
            FileWriter fw = new FileWriter(destFile.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            for(int i = 0; i < getDefaultTableModel().getRowCount(); i++) {
                for(int j = 0; j < getDefaultTableModel().getColumnCount(); j++) {
                    if(getDefaultTableModel().getValueAt(i, j) != null || !getDefaultTableModel().getValueAt(i, j).toString().trim().isEmpty()) {
                        bw.write(String.valueOf(getDefaultTableModel().getValueAt(i, j)));
                        if(j != (getDefaultTableModel().getColumnCount() - 1) ) {
                            bw.write(":");
                        }
                    }
                }
                bw.newLine();
            }
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        this.dispose();
//        MainDriver.generateSuggestion();
    }

    /**
     * Gets the default table model.
     *
     * @return the default table model
     */
    public DefaultTableModel getDefaultTableModel() {
        return (DefaultTableModel) table.getModel();
    }

}
